package com.hanhai.material.device.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBom;
import com.hanhai.material.device.entity.SysBomChild;
import com.hanhai.material.device.entity.SysWorkPlan;
import com.hanhai.material.device.service.SysBomService;
import com.hanhai.material.device.service.SysWorkPlanService;
import com.hanhai.material.device.utils.kingdee.KingdeeUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.hanhai.material.device.entity.table.SysBomTableDef.SYS_BOM;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2023/10/18 11:26:03
 */
@RestController
@RequestMapping("/bom")
public class SysBomController {

    @Autowired
    private SysBomService sysBomService;
    @Autowired
    private SysWorkPlanService sysWorkPlanService;

    @Autowired
    private KingdeeUtils kingdeeUtils;

    /**
     * 添加。
     *
     * @param sysBom
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody SysBom sysBom) {
        return sysBomService.save(sysBom);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return sysBomService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysBom
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody SysBom sysBom) {
        return sysBomService.updateById(sysBom);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<SysBom> list() {
        return sysBomService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @return 详情
     */
    @PostMapping("getInfo/{workPlanId}")
    public Result<?> getInfo(@PathVariable String workPlanId) throws IOException {
//        return sysBomService.getById(id);
        SysWorkPlan sysWorkPlan = this.sysWorkPlanService.getById(workPlanId);
        System.out.println("WorkPlan:" + sysWorkPlan);

        List<SysBomChild> sysBomChildList = new ArrayList<>();
        if (sysWorkPlan != null) {
            QueryWrapper queryWrapper = QueryWrapper.create();
            queryWrapper.where(SYS_BOM.PRODUCT_ICMO_ID.eq(sysWorkPlan.getInterId()));
            queryWrapper.orderBy(SYS_BOM.ID, false);
            queryWrapper.limit(1);
            SysBom sysBom = this.sysBomService.getOne(queryWrapper);
            System.out.println("SysBom:" + sysBom);
            if (sysBom != null) {
                sysBomChildList = this.kingdeeUtils.getPPBOMDetail(sysBom.getId());
            }
        }
        return sysBomChildList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysBomChildList);
    }

    @PostMapping("getInfoList")
    public Result<?> getInfo(@RequestBody List<SysWorkPlan> workPlanList) throws IOException {
        if (CollectionUtils.isEmpty(workPlanList)) {
            return ResultTool.fail();
        }
        List<Integer> workPlanIds = workPlanList.stream().map(SysWorkPlan::getInterId).collect(Collectors.toList());
//

        List<SysBomChild> sysBomChildList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(workPlanIds)) {
//            for (SysWorkPlan workPlan : workPlanList) {
            QueryWrapper queryWrapper = QueryWrapper.create();
            queryWrapper.where(SYS_BOM.PRODUCT_ICMO_ID.in(workPlanIds));
            queryWrapper.orderBy(SYS_BOM.ID, false);
            List<SysBom> sysBoms = this.sysBomService.getMapper().selectListWithRelationsByQuery(queryWrapper);
            for (SysBom sysBom : sysBoms) {
                if (sysBom != null) {
                    List<SysBomChild> bomChildList = this.kingdeeUtils.getPPBOMDetail(sysBom.getId());
                    bomChildList.forEach(child -> {
                        child.setWorkPlan(sysBom.getWorkPlan());
                    });
                    sysBomChildList.addAll(bomChildList);
                }
            }




        }
        return sysBomChildList.isEmpty() ? ResultTool.fail() : ResultTool.success(sysBomChildList);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysBom> page(Page<SysBom> page) {
        return sysBomService.page(page);
    }

}
